package cn.com.anypay.manager.common.utils;

/**
 * HTML工具类
 * 提供HTML转义和页面生成的工具方法
 */
public final class HtmlUtils {

    // 防止实例化
    private HtmlUtils() {}

    /**
     * HTML转义
     * 将特殊字符转换为HTML实体，防止XSS攻击
     * @param input 输入字符串
     * @return 转义后的字符串
     */
    public static String escapeHtml(String input) {
        if (StringUtils.isBlank(input)) {
            return input;
        }
        return input.replace("&", "&amp;")
                   .replace("<", "&lt;")
                   .replace(">", "&gt;")
                   .replace("\"", "&quot;")
                   .replace("'", "&#39;");
    }

    /**
     * 生成基础HTML页面
     * @param title 页面标题
     * @param content 页面内容
     * @return 完整的HTML页面
     */
    public static String generateBasicHtmlPage(String title, String content) {
        String safeTitle = escapeHtml(title);
        String safeContent = escapeHtml(content);
        
        return "<!DOCTYPE html>" +
               "<html lang='zh-CN'>" +
               "<head>" +
               "<meta charset='UTF-8'>" +
               "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
               "<title>" + safeTitle + "</title>" +
               "<style>" +
               "body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }" +
               ".container { max-width: 800px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }" +
               "h1 { color: #333; text-align: center; margin-bottom: 20px; }" +
               ".content { line-height: 1.6; color: #666; }" +
               "</style>" +
               "</head>" +
               "<body>" +
               "<div class='container'>" +
               "<h1>" + safeTitle + "</h1>" +
               "<div class='content'>" + safeContent + "</div>" +
               "</div>" +
               "</body>" +
               "</html>";
    }

    /**
     * 生成图片显示HTML页面
     * @param imageUrl 图片URL
     * @param title 页面标题（可选）
     * @return 图片显示HTML页面
     */
    public static String generateImageDisplayHtml(String imageUrl, String title) {
        String safeImageUrl = escapeHtml(imageUrl);
        String safeTitle = StringUtils.isBlank(title) ? "图片预览" : escapeHtml(title);
        
        return "<!DOCTYPE html>" +
               "<html lang='zh-CN'>" +
               "<head>" +
               "<meta charset='UTF-8'>" +
               "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
               "<title>" + safeTitle + "</title>" +
               "<style>" +
               "body { margin: 0; padding: 20px; background-color: #f0f0f0; font-family: Arial, sans-serif; }" +
               ".container { max-width: 100%; text-align: center; }" +
               "h1 { color: #333; margin-bottom: 20px; }" +
               ".image-wrapper { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); display: inline-block; }" +
               "img { max-width: 100%; height: auto; border-radius: 4px; }" +
               ".error { color: #e74c3c; padding: 20px; background: #fdf2f2; border: 1px solid #f5c6cb; border-radius: 4px; }" +
               "</style>" +
               "</head>" +
               "<body>" +
               "<div class='container'>" +
               "<h1>" + safeTitle + "</h1>" +
               "<div class='image-wrapper'>" +
               "<img src='" + safeImageUrl + "' alt='预览图片' onerror='this.parentElement.innerHTML=\"<div class=\\\"error\\\">图片加载失败，请检查图片链接是否有效</div>\";' />" +
               "</div>" +
               "</div>" +
               "</body>" +
               "</html>";
    }

    /**
     * 生成文本显示HTML页面
     * @param text 要显示的文本
     * @param title 页面标题（可选）
     * @return 文本显示HTML页面
     */
    public static String generateTextDisplayHtml(String text, String title) {
        String safeText = escapeHtml(text);
        String safeTitle = StringUtils.isBlank(title) ? "文本预览" : escapeHtml(title);
        
        return "<!DOCTYPE html>" +
               "<html lang='zh-CN'>" +
               "<head>" +
               "<meta charset='UTF-8'>" +
               "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
               "<title>" + safeTitle + "</title>" +
               "<style>" +
               "body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }" +
               ".container { max-width: 800px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }" +
               "h1 { color: #333; text-align: center; margin-bottom: 30px; border-bottom: 2px solid #eee; padding-bottom: 15px; }" +
               ".text-content { background: #f8f9fa; padding: 20px; border-radius: 6px; border-left: 4px solid #007bff; font-size: 16px; line-height: 1.6; color: #333; white-space: pre-wrap; word-wrap: break-word; }" +
               "</style>" +
               "</head>" +
               "<body>" +
               "<div class='container'>" +
               "<h1>" + safeTitle + "</h1>" +
               "<div class='text-content'>" + safeText + "</div>" +
               "</div>" +
               "</body>" +
               "</html>";
    }

    /**
     * 生成错误页面
     * @param errorMessage 错误信息
     * @param title 页面标题（可选）
     * @return 错误页面HTML
     */
    public static String generateErrorHtml(String errorMessage, String title) {
        String safeErrorMessage = escapeHtml(errorMessage);
        String safeTitle = StringUtils.isBlank(title) ? "错误" : escapeHtml(title);
        
        return "<!DOCTYPE html>" +
               "<html lang='zh-CN'>" +
               "<head>" +
               "<meta charset='UTF-8'>" +
               "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
               "<title>" + safeTitle + "</title>" +
               "<style>" +
               "body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }" +
               ".container { max-width: 600px; margin: 50px auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); text-align: center; }" +
               "h1 { color: #e74c3c; margin-bottom: 20px; }" +
               ".error-message { background: #fdf2f2; color: #721c24; padding: 15px; border: 1px solid #f5c6cb; border-radius: 4px; margin: 20px 0; }" +
               ".icon { font-size: 48px; color: #e74c3c; margin-bottom: 20px; }" +
               "</style>" +
               "</head>" +
               "<body>" +
               "<div class='container'>" +
               "<div class='icon'>⚠️</div>" +
               "<h1>" + safeTitle + "</h1>" +
               "<div class='error-message'>" + safeErrorMessage + "</div>" +
               "</div>" +
               "</body>" +
               "</html>";
    }
}